import { DebouncedFunc, debounce } from "lodash";
import { onBeforeUnmount } from "vue";

export function useDebounce<T extends (...args: unknown[]) => unknown>(
  fn: T,
  delay: number
): DebouncedFunc<T> {
  const debounced = debounce(fn, delay);

  onBeforeUnmount(() => {
    debounced.cancel();
  });

  return debounced;
}
